package com.xyzzlky.test;

import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;

public class BURewardedVideoAd {

    private Activity activity;
    private AdSlot adSlot;
    private AdListener adListener;
    private boolean isCall;

    private TTRewardVideoAd ad;
    private boolean rewardValid;

    public BURewardedVideoAd(Activity activity, String adUnitId, AdListener adListener) {
        this.activity = activity;
        this.adListener = adListener;
        Configuration config = activity.getResources().getConfiguration();
        adSlot = new AdSlot.Builder()
                .setCodeId(adUnitId)
                .setSupportDeepLink(true)
                .setAdCount(2)
                .setImageAcceptedSize(1080, 1920)
                .setExpressViewAcceptedSize(320, 640)
                .setRewardName("1") //奖励的名称
                .setRewardAmount(1) //奖励的数量
                //必传参数，表来标识应用侧唯一用户；若非服务器回调模式或不需sdk透传
                //可设置为空字符串
                .setUserID("")
                .setOrientation(config.orientation) //设置期望视频播放的方向，为TTAdConstant.HORIZONTAL或TTAdConstant.VERTICAL
                .setMediaExtra("media_extra") //用户透传的信息，可不传
                .build();
    }

    public void load(boolean isCall) {
        this.isCall = isCall;
        if (null != ad) {
            if (BURewardedVideoAd.this.isCall) {
                adListener.loadResult(true);
            }
            return;
        }
        BU.getTTAdNative(activity).loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int errCode, String errMsg) {
                Log.d("MyTag", errCode + "_" + errMsg);
                BURewardedVideoAd.this.ad = null;
                if (null != adListener) {
                    adListener.onError(errCode, errMsg);
                }
                if (BURewardedVideoAd.this.isCall) {
                    if (null != adListener) {
                        adListener.loadResult(false);
                    }
                }
            }

            @Override
            public void onRewardVideoCached() {
            }

            //视频广告素材加载到，如title,视频url等，不包括视频文件
            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
                BURewardedVideoAd.this.ad = ad;
                bindAdListener(ad);
                if (null != adListener) {
                    adListener.onLoad();
                }
                if (BURewardedVideoAd.this.isCall) {
                    if (null != adListener) {
                        adListener.loadResult(true);
                    }
                }
            }
        });

    }

    public void show() {
        if (null != ad) {
            ad.showRewardVideoAd(activity);
        } else {
            if (null != adListener) {
                adListener.showResult(false);
            }
        }
    }

    private void bindAdListener(TTRewardVideoAd ad) {
        ad.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                if (null != adListener) {
                    adListener.showResult(true);
                }
            }

            @Override
            public void onAdVideoBarClick() {
            }

            @Override
            public void onAdClose() {
                BURewardedVideoAd.this.ad = null;
                if (null != adListener) {
                    adListener.onClose(rewardValid);
                }
                rewardValid = false;
                load(false);
            }

            @Override
            public void onVideoError() {
                if (null != adListener) {
                    adListener.showResult(false);
                }
                BURewardedVideoAd.this.ad = null;
            }

            @Override
            public void onVideoComplete() {
            }

            @Override
            public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName) {
                rewardValid = rewardVerify;
            }

            @Override
            public void onSkippedVideo() {
            }
        });
    }

}
